import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_math_fork/flutter_math.dart';
import 'package:markdown/markdown.dart' as md;
import 'latex_syntax.dart';
import 'latex_builder.dart';
import 'custom_markdown_table_builder.dart';
import 'code_block_builder.dart';
import 'bracket_fraction_syntax.dart';
import 'bracket_fraction_builder.dart';

class MarkdownPreviewWidget extends StatelessWidget {
  final String content;
  final TextStyle? style;
  final int? maxLines;
  final TextOverflow overflow;
  final double? textScaleFactor;

  const MarkdownPreviewWidget({
    Key? key,
    required this.content,
    this.style,
    this.maxLines,
    this.overflow = TextOverflow.ellipsis,
    this.textScaleFactor,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 使用Markdown组件来渲染Markdown内容，包括表格
    return MarkdownBody(
      data: content,
      styleSheet: MarkdownStyleSheet(
        p: style?.copyWith(fontFamily: 'FangSong'),
        h1: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        h2: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        h3: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        h4: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        h5: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        h6: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        listBullet: style?.copyWith(fontFamily: 'FangSong'),
        tableBody: style?.copyWith(fontFamily: 'FangSong'),
        tableHead: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        code: style?.copyWith(fontFamily: 'FangSong'),
        blockquote: style?.copyWith(fontFamily: 'FangSong'),
        em: style?.copyWith(fontFamily: 'FangSong', fontStyle: FontStyle.italic),
        strong: style?.copyWith(fontFamily: 'FangSong', fontWeight: FontWeight.bold),
        textScaleFactor: textScaleFactor ?? 1.0,
      ),
      builders: {
        'table': CustomMarkdownTableBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'math': LatexBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'inline_math': InlineLatexBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'code': CodeBlockBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'code_block': CodeBlockBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'code_span': InlineCodeBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
        'bracket_fraction': BracketFractionBuilder(style: style, textScaleFactor: textScaleFactor ?? 1.0),
      },
      extensionSet: md.ExtensionSet(
        [...md.ExtensionSet.gitHubFlavored.blockSyntaxes, MathSyntax()],
        [...md.ExtensionSet.gitHubFlavored.inlineSyntaxes, InlineMathSyntax(), BracketFractionSyntax()],
      ),
    );
  }
}